package com.lyhengtongwl.zqsnews.ui.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lyhengtongwl.zqsnews.App;
import com.lyhengtongwl.zqsnews.R;
import com.lyhengtongwl.zqsnews.adapter.WithDrawRecordAdapter;
import com.lyhengtongwl.zqsnews.base.BaseActivity;
import com.lyhengtongwl.zqsnews.entity.NewsBaseEntity;
import com.lyhengtongwl.zqsnews.entity.WithDrawRecordEntity;
import com.lyhengtongwl.zqsnews.net.MyCallback;
import com.lyhengtongwl.zqsnews.net.Task;
import com.lyhengtongwl.zqsnews.utils.SPUtils;
import com.lyhengtongwl.zqsnews.utils.ScreenUtil;
import com.lyhengtongwl.zqsnews.utils.TitleBuilder;
import com.lyhengtongwl.putorefresh.BaseBothListener;
import com.lyhengtongwl.putorefresh.PullToBothLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Response;

public class NewsWithDrawRecordActivity extends BaseActivity {
    @BindView(R.id.ll_noRecord)
    LinearLayout ll_noRecord;//没有提现记录时显示
    @BindView(R.id.tv_withDrawNow)
    TextView withDrawNow;//立即提现

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.pullRefresh)
    PullToBothLayout pullToBothLayout;
    private List<WithDrawRecordEntity> list = new ArrayList<>();
    private int page = 1;
    private boolean isRefresh = true;//默认下拉刷新
    private WithDrawRecordAdapter newsAdapter = null;


    @OnClick({R.id.tv_withDrawNow})
    public void onViewClicked(View view) {
        if (ScreenUtil.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.tv_withDrawNow://立即提现
                startActivity(new Intent(this, NewsWithDrawNowActivity.class));
                break;
        }
    }

    @Override
    public void setListen() {
        pullToBothLayout.setRefreshListener(new BaseBothListener() {
            @Override
            public void refresh() {
                isRefresh = true;
                page = 1;
                getWithDrawRecord();
            }

            @Override
            public void loadMore() {
                isRefresh = false;
                page++;
                getWithDrawRecord();
            }
        });
    }

    @Override
    protected boolean isStatusBarBlack() {
        return true;
    }

    @Override
    public void setTitleBarColor() {
        new TitleBuilder(this).setTitleText("提现记录")
                .setLeftIcoListening(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_withdrawalrecord;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {
        list.clear();
        LinearLayoutManager manager = new LinearLayoutManager(this);
        if (newsAdapter == null) {
            newsAdapter = new WithDrawRecordAdapter(this, list);
        }
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(newsAdapter);
        getWithDrawRecord();
    }

    //获取提现记录
    private void getWithDrawRecord() {
        String userId = SPUtils.get(App.getContext(), "userId", "") + "";
        Task.getApiService().getWithDrawRecord(page, userId).enqueue(new MyCallback<NewsBaseEntity>(NewsWithDrawRecordActivity.this) {
            @Override
            protected void onSuccess(Response<NewsBaseEntity> response) {
                String jsonStr = JSONObject.toJSONString(response.body().getData());
                if (("1").equals(response.body().getCode())) {
                    JSONObject jsonObject = JSONObject.parseObject(jsonStr);
                    JSONArray jsonArray = jsonObject.getJSONArray("records");
                    if (isRefresh) {
                        if (jsonArray.size() > 0) {
                            ll_noRecord.setVisibility(View.GONE);
                            pullToBothLayout.setVisibility(View.VISIBLE);
                        } else {
                            ll_noRecord.setVisibility(View.VISIBLE);
                            pullToBothLayout.setVisibility(View.GONE);
                        }
                    }
                    if (isRefresh) {
                        //表示下拉刷新
                        list.clear();
                        for (int i = 0; i < jsonArray.size(); i++) {
                            WithDrawRecordEntity earnEntity = new WithDrawRecordEntity();
                            earnEntity.setMoney(jsonArray.getJSONObject(i).getInteger("money") + "");
                            earnEntity.setTitle(jsonArray.getJSONObject(i).getString("remark"));
                            earnEntity.setTime(jsonArray.getJSONObject(i).getString("updateTime"));
                            earnEntity.setState(jsonArray.getJSONObject(i).getInteger("status") + "");
                            list.add(earnEntity);
                        }
                        pullToBothLayout.finishRefresh();
                        newsAdapter.notifyDataSetChanged();
                    } else {
                        //上拉加载
                        for (int i = 0; i < jsonArray.size(); i++) {
                            WithDrawRecordEntity earnEntity = new WithDrawRecordEntity();
                            earnEntity.setMoney(jsonArray.getJSONObject(i).getInteger("money") + "");
                            earnEntity.setTitle(jsonArray.getJSONObject(i).getString("remark"));
                            earnEntity.setTime(jsonArray.getJSONObject(i).getString("updateTime"));
                            earnEntity.setState(jsonArray.getJSONObject(i).getInteger("status") + "");
                            list.add(earnEntity);
                        }
                        pullToBothLayout.finishLoadMore();
                        newsAdapter.notifyDataSetChanged();
                    }
                }
            }

            @Override
            protected void onFailure(Throwable t) {

            }
        });
    }
}
